<template>
    <div>
        <div class="box">
            <Form :model="formItem" :label-width="80">
                <Row>
                    <Col span="5">
                    <FormItem label="开始日期">
                      <DatePicker type="date"  :options="options1" v-model="formItem.start" @on-change="geTimestart"></DatePicker>
                    </FormItem>
                    </Col>
                    <Col span="5">
                    <FormItem label="结束日期">
                      <DatePicker type="date" :options="options2"  v-model="formItem.end" @on-change="geTimend"></DatePicker>
                    </FormItem>
                    </Col>
                    <Col span="5">
                    <FormItem label="状态">
                        <Select v-model="formItem.status">
                            <Option v-for="item in formItem.statusArr" :value="item" :key="item">{{ item}}</Option>
                        </Select>
                    </FormItem>
                    </Col>

                    <Col span="2" offset="1">
                     <Button type="primary" @click="searchBtn">查询</Button>
                    </Col>

                </Row>
            </Form>

            <Table border :columns="columns" :data="datas"></Table>

            <Page :total="pageTotal"
                  pageSize="30"
                  size="small"
                  @on-change="searchBtnPage"
                  show-elevator
                  show-total>
            </Page>
        </div>

        <Modal v-model="modalDel"
               title="云商配置-删除"
               :loading="loading"
               @on-ok="delOk"
               @on-cancel="delCancel">
            确定删除？
        </Modal>

    </div>
</template>
<script>
    import * as Button from '../../lib/actionBtn'
    import * as fuc from '../../lib/lib'
    import * as Url from '../../api/url'
    import {MessageApi} from '../../api/api'
    import axios from 'axios'

    export default {
        data () {
            return {
                current:'messageList',  //当前页
                columns: [
                    {title:'消息ID',key: 'id',width:80},
                    {title:'消息标题',key: 'title'},
                    {title:'焦点消息',key: 'isFocus',width:90},//msg
                    {title:'外部浏览器打开',key: 'targetBrowser',width:130},//msg
                    {title:'发送时间',key: 'sendTime'},  //send_time
                    {title:'创建时间',key: 'createTime'}, //creat_time
                    {title:'状态',  key:'status',width:80},
                    {title:'审核状态',key:'isReview',width:90,},  //audit_status
                    {title:'发送数量',key:'sendCount'},    //sendNum
                    {title:'查看详情数量',key:'viewCount'}, //seeNum
                    {title: '操作', key: 'action', width: 250, align: 'center',
                        render: (h, params) => {
                            return  this.action(h,params)
                        }
                    }
                ],
                datas: [],
                pageTotal:'',     //总页数
                pageNum:'0',       //当前分页
                formItem:{
                    start:'',      //开始时间
                    end:'',        //结束
                    statusDefault:2,
                    status:'全部',
                    statusArr:["全部","已发送","待发送","发送中"]
                },
                modalDel:false,
                modalDelId:'',
                delIndex:'',
                passId:'',   //审核id
                passBtn:'审核通过',   //审核通过
                passArr:[],   //存 审核颜色 点击状态
                options1:{   //开始
                  disabledDate (date) {
                    return date.getTime() > Date.now();
                  }
                /*  disabledDate: (time) => {
                    let beginDateVal = this.formItem.start;
                    if (beginDateVal) {
                      return time.getTime() > beginDateVal;
                    }
                  }*/
                },
                options2:{   //结束
                  disabledDate (date) {
                    return date.getTime() < Date.now()- 86400000;
                  }
                 /* disabledDate: (time) => {
//                    let beginDateVal = this.formItem.end;
                    let beginDateVal = this.formItem.start;
                    if (beginDateVal) {
                      return time.getTime() < beginDateVal;
                    }
                  }*/
                }
            }
        },

        mounted:function () {
            this.$nextTick(function () {
              this.pageChange(this.pageNum)
            })
        },
        computed: {

        },
        methods: {
            geTimestart(date){
              this.formItem.start=date;
            },
            geTimend(date){
              this.formItem.end=date;
            },
            ApiMessage(getData){
              this.datas=[];
              MessageApi.getMessage({params:getData}).then(response=>{
                if(response.messageCode==200){
                  if(response.data.data !=undefined && this.datas !=null && response.data.data.length>0){
                    this.datas=response.data.data;
                    this.pageTotal=response.data.totalCount;
                    this.datas.map( v=>{
                      switch (v.isReview){
                        case 0:v.isReview="未通过";break;
                        case 1:v.isReview="通过";break;
                      }
                      switch (v.isFocus){
                        case 0:v.isFocus="否";break;
                        case 1:v.isFocus="是";break;
                      }
                      switch (v.targetBrowser){
                        case 1:v.targetBrowser="否";break;
                        case 2:v.targetBrowser="是";break;
                      }
                      switch (v.status){
                        case 0:v.status="待发送";break;
                        case 1:v.status="已发送";break;
                        case 3:v.status="发送中";break;
                        case -1:v.status="已删除";break;
                      }
                      this.passArr.push({
                        "passColor":'warning',  //审核按钮颜色
                        "disabled":false       //是否可以点击
                      })
                    });
                  }else{
                    this.pageTotal=0;
                    //this.$Message.success('查询失败')
                  }
                }

              }).catch( error=>{
                this.pageTotal=0;
                //this.$Message.error('查询失败')
              });
            },

            pageChange(page){
              this.pageNum=page;
              this.formItem.start=fuc.dateFormatYMD(fuc.dateForward(30));
              this.formItem.end=fuc.dateFormatYMD(new Date());
              this.formItem.status =  this.formItem.statusDefault;
              let data={
                beginDate:this.formItem.start,
                endDate:this.formItem.end,
                status: this.formItem.status,
                pageNo:this.pageNum
              };
              this.ApiMessage(data);
            },
            dataAction(){
              switch (this.formItem.status){
                case "全部"  :this.formItem.status=2;break;
                case "已发送":this.formItem.status=1;break;
                case "待发送":this.formItem.status=0;break;
                case "发送中":this.formItem.status=3;break;
              }
              if(this.formItem.start !="NaN-NaN-NaN" && this.formItem.start !=""){
                this.formItem.start =fuc.dateFormatYMD(this.formItem.start);
                if(this.formItem.start=="1970-01-01"){
                  this.formItem.start =null;
                }
              }else {
                this.formItem.start =null;
              }
              if(this.formItem.end !="NaN-NaN-NaN" && this.formItem.end !="" ){
                this.formItem.end =fuc.dateFormatYMD(this.formItem.end);
                if(this.formItem.end=="1970-01-01"){
                  this.formItem.end =null;
                }
              }else {
                this.formItem.end =null;
              }
              let data={
                beginDate:this.formItem.start,
                endDate:this.formItem.end,
                status: this.formItem.status,
                pageNo:this.pageNum
              };
              this.ApiMessage(data);
            },
            searchBtnPage(page){
              this.pageNum =page;
              this.dataAction();
            },
            searchBtn(){
              this.pageNum =0;
              this.pageTotal=0;
              this.dataAction();
            },

            removeModal(index){
                this.modalDel=true;
                this.delIndex=index;
                this.modalDelId=this.datas[index].id;

            },
            delOk(){
              this.modalDel=false;
              MessageApi.delMessage(Url.message+this.modalDelId).then( response=>{
                if(response.messageCode==200){
                  this.datas.splice(this.delIndex, 1);
                  this.$Message.success(this.modalDelId+' 删除成功');
                }else{
                  this.$Message.error(response.description)
                }
              }).catch( error=>{
              });
            },
            delCancel(){
                this.modalDel=false;
            },

            action(h,params){
                this.status =params.row.status;
                this.isReview =params.row.isReview;
                if(this.status=="已发送" || this.status=="发送中"){
                    return  [
                        Button.allBtns.btnInfo(this,h,params)
                    ]
                }else if(this.status=="待发送" && this.isReview=="未通过"){
                    return  [
                         Button.allBtns.btnPassed(this,h,params),
                        Button.allBtns.btnEdit(this,h,params),
                        Button.allBtns.btnInfo(this,h,params),
                        Button.allBtns.btnDel(this,h,params)
                    ]
                }else if(this.status=="待发送" && this.isReview=="通过"){
                  return  [
                    Button.allBtns.btnEdit(this,h,params),
                    Button.allBtns.btnInfo(this,h,params),
                    Button.allBtns.btnDel(this,h,params)
                  ]
                }

            },

          couponPass(index){
              this.passId=this.datas[index].id;
             axios.put(Url.messagePass+this.passId).then( response=>{
              if(response.data.messageCode==200){
                this.passArr[index].passColor='Default';
                this.passArr[index].disabled=true;
                this.passBtn='审核通过';

                this.datas[index].isReview='通过'
                this.searchBtn();
                this.$Message.success(this.passId+' 审核通过成功');
              }else{
                this.$Message.error('审核失败')
              }
            }).catch( error=>{
            });
          }

        }
    }
</script>
<style scoped>
    .box{padding:10px;}
    .ivu-input[disabled], fieldset[disabled] .ivu-input {background-color: #f3f3f3;opacity: 1;cursor: not-allowed;color: #6b6b6b;}
    .ivu-page {margin-top: 10px;}
</style>
